package com.coincalf.config;

import com.coincalf.framework.enums.MarketType;
import com.coincalf.service.EntrustOrderService;
import com.coincalf.service.MarketService;
import com.coincalf.service.TurnoverOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Author: Chen Long
 * @Date: Created in 2018/5/29 下午6:37
 * @Modified by: Chen Long
 */
@Component
@Slf4j
public class InitRunner implements CommandLineRunner {

    @Autowired
    private MarketService marketService;
    @Autowired
    private EntrustOrderService entrustOrderService;
    @Autowired
    private TurnoverOrderService turnoverOrderService;

    @Override
    public void run(String... args) throws Exception {
        log.info("缓存数据初始化开始");
        long start = System.currentTimeMillis();
        marketService.queryByType(MarketType.TRADE).forEach(market -> {
            // 币币交易行情初始化
            marketService.TRADE_MARKET_CACHE_INSTANCE().refresh(market.getId());
            // 币币交易深度初始化
            entrustOrderService.DEPTH_CACHE_INSTANCE().refresh(market.getId());
            // 币币交易合并深度初始化
            entrustOrderService.MERGE_LOW_DEPTH_CACHE_INSTANCE().refresh(market.getId());
            // 币币交易合并深度初始化
            entrustOrderService.MERGE_HIGH_DEPTH_CACHE_INSTANCE().refresh(market.getId());
            // 币币交易最新成交初始化
            turnoverOrderService.DEAL_ORDER_CACHE_INSTANCE().refresh(market.getId());
        });
        // 创新交易行情初始化
        marketService.queryByType(MarketType.FOREX).forEach(market -> {
            marketService.FOREX_MARKET_CACHE_INSTANCE().refresh(market.getId());
        });
        long end = System.currentTimeMillis();
        log.info("缓存数据初始化完成，耗时：{} ms", (end - start));
    }

}
